package com.hk.yajin.manager;

import android.content.Intent;
import android.text.TextUtils;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.developmentlib.utills.DeviceUtils;
import com.hk.developmentlib.utills.FastjsonUtils;
import com.hk.developmentlib.utills.PreferUtil;
import com.hk.developmentlib.utills.constants.BroadcastConstants;
import com.hk.developmentlib.utills.constants.PreferConstant;
import com.hk.yajin.base.MyApplication;
import com.hk.yajin.function.MainActivity;
import com.hk.yajin.model.UserInfo;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.MessageHelper;
import com.hk.yajin.utills.url.UrlConstants;
import com.umeng.message.PushAgent;

import java.util.HashMap;
import java.util.Map;


/**
 * 类描述：
 * 创建人：wl
 * 创建时间：2017/7/10 23:13
 */
public class UserInfoManager {

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    private UserInfo userInfo = new UserInfo();

    private String loginkey = "";

    private boolean isLogin = false;


    public UserInfoManager() {
        autoLogin();
    }

    public static Map<String, Object> getUserKeyMapIfLogin() {
        Map<String, Object> map = new HashMap<>();
        if (getInstance().isLogin()) {
            map.put("loginkey", getInstance().getLoginkey());
        }
        return map;
    }

    private void autoLogin() {
        String account = PreferUtil.getString(PreferConstant.LOGIN_ACCOUNT, "");
        String pwd = PreferUtil.getString(PreferConstant.LOGIN_PASSWORD, "");
        boolean state = PreferUtil.getBoolean(PreferConstant.LOGIN_STATE, false);
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(pwd) && state) {
            httpLogin(account, pwd);
        }
    }


    private void httpLogin(final String acount, final String pwd) {
        Map<String, Object> reqParam = new HashMap<String, Object>();
        reqParam.put("mobile", acount);
        reqParam.put("password", pwd);
        reqParam.put("device_token", PushAgent.getInstance(MyApplication.getInstance()).getRegistrationId());
        HttpRequestUtils.post(false, UrlConstants.FUNCTION_LOGIN, reqParam, false, new HttpCallBack() {
                    @Override
                    public void onSucced(String data) {
                        UserInfo userInfo = FastjsonUtils.getBeanObject(data, UserInfo.class);
                        UserInfoManager.getInstance().loginSucceed(acount, pwd, userInfo);
                    }

                    @Override
                    public void onErrorCode(NewWorkBean bean) {

                    }

                    @Override
                    public void onBadHttp(String tip) {

                    }

                }

        );
    }

    private void setUserInfoResult(UserInfo info) {
        if (info != null) {
            userInfo = info;
            loginkey = info.getLoginkey();
            isLogin = true;
        }
    }


    public void loginSucceed(String account, String pwd, UserInfo info) {
        PreferUtil.putBoolean(PreferConstant.LOGIN_STATE, true);
        PreferUtil.putString(PreferConstant.LOGIN_ACCOUNT, account);
        PreferUtil.putString(PreferConstant.LOGIN_PASSWORD, pwd);
        setUserInfoResult(info);
        MessageHelper.restDataBaseId();
        MyApplication.getInstance().sendBroadcast(new Intent(BroadcastConstants.ACTION_USER_LOGIN));
    }


    public void exitLogin() {
        isLogin = false;
        loginkey = "";
        userInfo = new UserInfo();
        PreferUtil.putBoolean(PreferConstant.LOGIN_STATE, false);
        MessageHelper.restDataBaseId();
        MyApplication.getInstance().sendBroadcast(new Intent(BroadcastConstants.ACTION_USER_LOGINOUT));
    }


    public String getLoginkey() {
        return loginkey;
    }

    public boolean isLogin() {
        return isLogin;
    }


    public static UserInfoManager getInstance() {
        return UserInfoManagerHolder.USER_INFO_MANAGER;
    }

    private static class UserInfoManagerHolder {
        private static final UserInfoManager USER_INFO_MANAGER = new UserInfoManager();
    }
}
